//
//  DrawingPath.m
//  TabulaImaginarius2
//
//  Created by TA2 on 5/17/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "DrawingPath.h"


@implementation DrawingPath

@synthesize points;
@synthesize newPoints;
@synthesize	color;
@synthesize colorTexturePath;
@synthesize	lineWidth;

- (id)init {
	points = [[NSMutableArray alloc] init];
    newPoints = [[NSMutableArray alloc] init];
	color = [UIColor blackColor];
	lineWidth = 7.0;
	return self;
}

- (void)dealloc {
    [points release];
    points = nil;
    [newPoints release];
    newPoints = nil;
    [color release];
    color = nil;
    [colorTexturePath release];
    colorTexturePath = nil;
	[super dealloc];
}

+ (id)arrayWithArray:(DrawingPath *)anArray {
	DrawingPath *newArray = [[[DrawingPath alloc] init] autorelease];
	newArray.points = [NSArray arrayWithArray:anArray.points];
    newArray.newPoints = [NSArray arrayWithArray:anArray.newPoints];
	newArray.color = anArray.color;
	newArray.lineWidth = anArray.lineWidth;
	return newArray;
}

- (void)encodeWithCoder:(NSCoder *)coder {
    if (self.points) {
        [coder encodeInt:[points count] forKey:@"drawingPathPointsCount"];
        int index = 0;
        for (NSValue *pointValue in self.points) {
            CGPoint point = [pointValue CGPointValue];
            [coder encodeCGPoint:point forKey:[@"drawingPathPoint" stringByAppendingString:[NSString stringWithFormat:@"%d", index]]];
            index++;
        }
    } else {
        [coder encodeInt:0 forKey:@"drawingPathPointsCount"];
    }
    [coder encodeObject:self.colorTexturePath forKey:@"drawingPathColorTexturePath"];
    if (!colorTexturePath) {
        [coder encodeObject:self.color forKey:@"drawingPathColor"];
    }
    [coder encodeFloat:self.lineWidth forKey:@"drawingPathLineWidth"];
}

- (id)initWithCoder:(NSCoder *)coder {
    self = [super init];
    self.points = [[NSMutableArray alloc] init];
    self.newPoints = [[NSMutableArray alloc] init];
    int count = [coder decodeIntForKey:@"drawingPathPointsCount"];
    for (int i = 0; i < count; i++) {
        [points addObject:[NSValue valueWithCGPoint:[coder decodeCGPointForKey:[@"drawingPathPoint" stringByAppendingString:[NSString stringWithFormat:@"%d", i]]]]];
    }
    self.colorTexturePath = [coder decodeObjectForKey:@"drawingPathColorTexturePath"];
    if (colorTexturePath) {
        self.color = [UIColor colorWithPatternImage:[UIImage imageNamed:colorTexturePath]];
    } else {
        self.color = [coder decodeObjectForKey:@"drawingPathColor"];
    }
    self.lineWidth = [coder decodeFloatForKey:@"drawingPathLineWidth"];
    return self;
}

@end
